package org.example.springoauth2.securitycommon.config.authenticate;

import lombok.AllArgsConstructor;
import org.example.springoauth2.securitycommon.config.authenticate.handler.CustomizeAuthenticationFailureHandler;
import org.example.springoauth2.securitycommon.config.authenticate.handler.CustomizeAuthenticationSuccessHandler;
import org.example.springoauth2.securitycommon.config.authenticate.grant.phone.PhoneAuthenticationProcessingFilter;
import org.example.springoauth2.securitycommon.config.authenticate.grant.phone.PhoneAuthenticationProvider;
import org.example.springoauth2.securitycommon.config.authenticate.grant.tt.TtAuthenticationProcessingFilter;
import org.example.springoauth2.securitycommon.config.authenticate.grant.tt.TtAuthenticationProvider;
import org.example.springoauth2.securitycommon.config.authenticate.grant.wx.WxAuthenticationProcessingFilter;
import org.example.springoauth2.securitycommon.config.authenticate.grant.wx.WxAuthenticationProvider;
import org.example.springoauth2.securitycommon.service.SysUserService;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.SecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.DefaultSecurityFilterChain;
import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

/**
 * @author hzq
 * @since 2021-09-12
 */
@AllArgsConstructor
public class CustomizeAuthorizationSecurityConfig extends SecurityConfigurerAdapter<DefaultSecurityFilterChain, HttpSecurity> {

    private final SysUserService sysUserService;
    private final CustomizeAuthenticationFailureHandler customizeAuthenticationFailureHandler;
    private final CustomizeAuthenticationSuccessHandler customizeAuthenticationSuccessHandler;
    private final AuthenticationManager authenticationManager;

    @Override
    public void configure(HttpSecurity builder) throws Exception {
        // 微信小程序登录
        WxAuthenticationProcessingFilter wxAuthenticationProcessingFilter = new WxAuthenticationProcessingFilter();
        configure(wxAuthenticationProcessingFilter);
        WxAuthenticationProvider wxAuthenticationProvider = new WxAuthenticationProvider(sysUserService);

        // 抖音小程序登录
        TtAuthenticationProcessingFilter ttAuthenticationProcessingFilter = new TtAuthenticationProcessingFilter();
        configure(ttAuthenticationProcessingFilter);
        TtAuthenticationProvider ttAuthenticationProvider = new TtAuthenticationProvider(sysUserService);

        // 手机号码登录
        PhoneAuthenticationProcessingFilter phoneAuthenticationProcessingFilter = new PhoneAuthenticationProcessingFilter();
        configure(phoneAuthenticationProcessingFilter);
        PhoneAuthenticationProvider phoneAuthenticationProvider = new PhoneAuthenticationProvider(sysUserService);

        builder.authenticationProvider(wxAuthenticationProvider)
                .addFilterAfter(wxAuthenticationProcessingFilter, UsernamePasswordAuthenticationFilter.class)

                .authenticationProvider(ttAuthenticationProvider)
                .addFilterAfter(ttAuthenticationProcessingFilter, UsernamePasswordAuthenticationFilter.class)

                .authenticationProvider(phoneAuthenticationProvider)
                .addFilterAfter(phoneAuthenticationProcessingFilter, UsernamePasswordAuthenticationFilter.class);
    }

    private void configure(AbstractAuthenticationProcessingFilter filter) {
        // 设置自定义登录失败处理器
        filter.setAuthenticationFailureHandler(customizeAuthenticationFailureHandler);
        // 设置自定义登录成功处理器
        filter.setAuthenticationSuccessHandler(customizeAuthenticationSuccessHandler);
        // 设置认证管理
        filter.setAuthenticationManager(authenticationManager);
    }

}
